

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DomUtils {
	public static Element[] getElements(Element element){
		NodeList nodes = element.getChildNodes();
        if(nodes != null){
            Element[] elements  = new Element[nodes.getLength()];
            for(int i = 0;i<nodes.getLength();i++){
            	Node node = nodes.item(i);
            	if(node.getNodeType() == Node.ELEMENT_NODE){
            		elements[i] = (Element)node;
            	}
            }
            return elements;
        }
        return null;
	}
	
	public static Element getFirstElements(Element element){
		Element[] elements = getElements(element);
		if(element != null && elements.length > 0){
			return elements[0];
		}else{
			return null;
		}
	}

	
	public static Element getElementByName(Element element,String localName){
		NodeList nodes = element.getChildNodes();
        if(nodes != null){
            for(int i = 0;i<nodes.getLength();i++){
            	Node node = nodes.item(i);
            	if(node.getNodeType() == Node.ELEMENT_NODE){
            		if(localName.equals(((Element)node).getLocalName())){
            			return (Element)node;
            		}
            	}
            }
        }
        return null;
	}
	
}
